import { _decorator, Component, Node, Sprite, UITransform } from 'cc'
import { randomString } from '../../Utils'
import { StateMachine } from '../../Base/StateMachine'
import {
  DIRECTION_ENUM,
  DIRECTION_ORDER_ENUM,
  ENTITY_STATE_ENUM,
  ENTITY_TYPE_ENUM,
  EVENT_ENUM,
  PARAMS_NAME_ENUM,
  SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM,
} from '../../Enum'
import { IEntity, ISpikes } from '../../Levels'
import { TILE_HEIGHT, TILE_WIDTH } from '../Tile/TileManager'
import { SpikesStateMachine } from './SpikesStateMachine'
import EventManager from '../../Runtime/EventManager'
import DataManager from '../../Runtime/DataManager'
const { ccclass, property } = _decorator

/**
 * Predefined variables
 * Name = SpikesManager
 * DateTime = Fri Jul 22 2022 16:32:00 GMT+0800 (中国标准时间)
 * Author = Zero_Y
 * FileBasename = SpikesManager.ts
 * FileBasenameNoExtension = SpikesManager
 * URL = db://assets/Scripts/Spikes/SpikesManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('SpikesManager')
export class SpikesManager extends Component {
  id: string = randomString(12)
  // 坐标
  x: number = 0
  y: number = 0
  // 状态机
  fsm: StateMachine
  // 地刺当前点数
  private _count: number
  // 地刺总点数
  private _totalCount: number

  type: ENTITY_TYPE_ENUM

  get count() {
    return this._count
  }
  set count(newCount) {
    this._count = newCount
    // 设置fsm化动画
    this.fsm.setParams(PARAMS_NAME_ENUM.SPIKES_CUR_COUNT, newCount)
  }
  get totalCount() {
    return this._totalCount
  }
  set totalCount(newCount) {
    this._totalCount = newCount
    this.fsm.setParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT, newCount)
  }

  async init(params: ISpikes) {
    const sprite = this.addComponent(Sprite)
    sprite.sizeMode = Sprite.SizeMode.CUSTOM
    const transform = this.getComponent(UITransform)
    transform.setContentSize(TILE_WIDTH * 4, TILE_HEIGHT * 4)

    // 设置fsm化动画
    this.fsm = this.addComponent(SpikesStateMachine)
    await this.fsm.init()
    this.x = params.x
    this.y = params.y
    this.type = params.type
    this.count = params.count
    this.totalCount = SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[this.type]

    EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END, this.onLoop, this)
  }
  onDestroy() {
    EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END, this.onLoop)
  }
  // 改变地刺个数
  onLoop() {
    if (this.count === this.totalCount) {
      this.count = 1
    } else {
      this.count++
    }
    this.onAttack()
  }
  // 判断地刺是否将角色杀死
  onAttack() {
    if (!DataManager.Instance.player) {
      return
    }

    const { x: playerX, y: playerY } = DataManager.Instance.player
    if (this.x === playerX && this.y === playerY && this.count === this.totalCount) {
      EventManager.Instance.emit(EVENT_ENUM.ATTACK_PLAYER, ENTITY_STATE_ENUM.DEATH)
    }
  }
  backZero() {
    this.count = 0
  }
  update() {
    // 更新移动位置，由于人物占4个格子居中需要偏移
    this.node.setPosition(this.x * TILE_WIDTH - TILE_WIDTH * 1.5, -this.y * TILE_HEIGHT + TILE_HEIGHT * 1.5)
  }
}
